{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2be4eb00",
   "metadata": {},
   "source": [
    "----\n",
    "* 时间：2023/5/18/week13 \n",
    "* 课程：API、机器学习与人工智能   \n",
    "* 学习记录人：林鑫磊  \n",
    "* 学习主题：语音识别+自然语言处理实践（对话机器人实现原理）\n",
    "----"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c0d355b",
   "metadata": {},
   "source": [
    "# 1.音频录制\n",
    "* 安装下载如下3个模块：pip install模块名称\n",
    "> 1.SpeechRecognition  \n",
    "> 2.Pyaudio   \n",
    "> 3.pydub  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fca511aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3d0513a",
   "metadata": {},
   "source": [
    "# 2.语音识别\n",
    "> 1.百度API：百度的AI账户-（Key,secret）      \n",
    "> 2.I/O:input:音频文件（pcm/wav/amr）  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "aa47629d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7a50678b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"refresh_token\":\"25.ee1bbeb253c66a5eb4bd1f6981917647.315360000.1999771104.282335-33415631\",\"expires_in\":2592000,\"session_key\":\"9mzdCyEmbfbTd8eOSJASd9TMXVvw0pO+PRW6K9+IuClOM6zt1dMSBjMq6TEgpgr3V982cfHKGfGnPcLe76PCHR4qJRclMQ==\",\"access_token\":\"24.09f94c24ba54b3009d3460560c374f86.2592000.1687003104.282335-33415631\",\"scope\":\"audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\\u6743\\u9650 vis-classify_flower lpq_\\u5f00\\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\\u5f00\\u653eScope vis-ocr_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 idl-video_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx\",\"session_secret\":\"b015afe229a47c2401fcd67ad652bbb2\"}\n",
      "\n",
      "{'refresh_token': '25.ee1bbeb253c66a5eb4bd1f6981917647.315360000.1999771104.282335-33415631', 'expires_in': 2592000, 'session_key': '9mzdCyEmbfbTd8eOSJASd9TMXVvw0pO+PRW6K9+IuClOM6zt1dMSBjMq6TEgpgr3V982cfHKGfGnPcLe76PCHR4qJRclMQ==', 'access_token': '24.09f94c24ba54b3009d3460560c374f86.2592000.1687003104.282335-33415631', 'scope': 'audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': 'b015afe229a47c2401fcd67ad652bbb2'}\n",
      "audio_voice_assistant_get\n",
      "SUCCESS WITH TOKEN: 24.09f94c24ba54b3009d3460560c374f86.2592000.1687003104.282335-33415631  EXPIRES IN SECONDS: 2592000\n",
      "Request time cost 0.991761\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'北京科技馆。'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bdasr_result = eval(bdasr.asr('16k.wav'))['result'][0]\n",
    "bdasr_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a704ec6",
   "metadata": {},
   "source": [
    "# 3.连接自动回复字典（openai-chatgpt）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "227be42e",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    '你是谁？':'我是集智慧和美貌于一身的智能小网同学',\n",
    "    '你好。':'你也好，今天听起来心情不错哦',\n",
    "    '北京科技馆。':'北京科技馆位于北京市朝阳区北辰东路5号，东临亚运居住区，西濒奥运水系，南依奥运主体育场，北望森林公园，于1958年筹建，建设分为两期工程和新馆工程，分别于1988年9月22日、2000年4月29日、2009年9月16日开放。'\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9952835d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'北京科技馆位于北京市朝阳区北辰东路5号，东临亚运居住区，西濒奥运水系，南依奥运主体育场，北望森林公园，于1958年筹建，建设分为两期工程和新馆工程，分别于1988年9月22日、2000年4月29日、2009年9月16日开放。'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qa_result = qa.get(bdasr_result)\n",
    "qa_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cc77a96",
   "metadata": {},
   "source": [
    "## 3.1接入Chatgtpt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93dbd64b",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install openai\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "506d1e2f",
   "metadata": {},
   "source": [
    "# 4.语音合成\n",
    "> 1.百度API：百度的AI账户-（Key,secret）         \n",
    "> 2.I/O:input:文本 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6583045b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SUCCESS WITH TOKEN: 24.f9011e79d4427829537a6ee33a13c5c0.2592000.1687003127.282335-33415631 ; EXPIRES IN SECONDS: 2592000\n",
      "test on Web Browserhttp://tsn.baidu.com/text2audio?tok=24.f9011e79d4427829537a6ee33a13c5c0.2592000.1687003127.282335-33415631&tex=%25E5%258C%2597%25E4%25BA%25AC%25E7%25A7%2591%25E6%258A%2580%25E9%25A6%2586%25E4%25BD%258D%25E4%25BA%258E%25E5%258C%2597%25E4%25BA%25AC%25E5%25B8%2582%25E6%259C%259D%25E9%2598%25B3%25E5%258C%25BA%25E5%258C%2597%25E8%25BE%25B0%25E4%25B8%259C%25E8%25B7%25AF5%25E5%258F%25B7%25EF%25BC%258C%25E4%25B8%259C%25E4%25B8%25B4%25E4%25BA%259A%25E8%25BF%2590%25E5%25B1%2585%25E4%25BD%258F%25E5%258C%25BA%25EF%25BC%258C%25E8%25A5%25BF%25E6%25BF%2592%25E5%25A5%25A5%25E8%25BF%2590%25E6%25B0%25B4%25E7%25B3%25BB%25EF%25BC%258C%25E5%258D%2597%25E4%25BE%259D%25E5%25A5%25A5%25E8%25BF%2590%25E4%25B8%25BB%25E4%25BD%2593%25E8%2582%25B2%25E5%259C%25BA%25EF%25BC%258C%25E5%258C%2597%25E6%259C%259B%25E6%25A3%25AE%25E6%259E%2597%25E5%2585%25AC%25E5%259B%25AD%25EF%25BC%258C%25E4%25BA%258E1958%25E5%25B9%25B4%25E7%25AD%25B9%25E5%25BB%25BA%25EF%25BC%258C%25E5%25BB%25BA%25E8%25AE%25BE%25E5%2588%2586%25E4%25B8%25BA%25E4%25B8%25A4%25E6%259C%259F%25E5%25B7%25A5%25E7%25A8%258B%25E5%2592%258C%25E6%2596%25B0%25E9%25A6%2586%25E5%25B7%25A5%25E7%25A8%258B%25EF%25BC%258C%25E5%2588%2586%25E5%2588%25AB%25E4%25BA%258E1988%25E5%25B9%25B49%25E6%259C%258822%25E6%2597%25A5%25E3%2580%25812000%25E5%25B9%25B44%25E6%259C%258829%25E6%2597%25A5%25E3%2580%25812009%25E5%25B9%25B49%25E6%259C%258816%25E6%2597%25A5%25E5%25BC%2580%25E6%2594%25BE%25E3%2580%2582&per=4&spd=5&pit=5&vol=5&aue=6&cuid=123456PYTHON&lan=zh&ctp=1\n",
      "result saved as :result.wav\n"
     ]
    }
   ],
   "source": [
    "import bdtts\n",
    "bdtts.tts(qa_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e3d0bd0",
   "metadata": {},
   "source": [
    "# 5.播放音频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3a21ceb",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('1.wav')\n",
    "play(song)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
